package com.proj.controller;

import com.proj.bean.Cart;
import com.proj.bean.User;
import com.proj.bean.Useraddress;
import com.proj.service.ICartService;
import com.proj.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("cart")
public class CartController {

    @Autowired
    ICartService cartService;

    @ResponseBody
    @RequestMapping("list")
    public List<Cart> getList(HttpSession session){
//        User u=(User) session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        if(session.getAttribute(SYS.SHOPPING_LOGIN_USER) != null){
            return cartService.getList(((User) session.getAttribute(SYS.SHOPPING_LOGIN_USER)).getId());
        }else{
            return null;
        }

//        return cartService.getList(u.getId());
    }

    @ResponseBody
    @RequestMapping("del")
    public int del(int goodsid, HttpSession session){
        return cartService.del(new Cart(((User) session.getAttribute(SYS.SHOPPING_LOGIN_USER)).getId(),goodsid));
    }

    @ResponseBody
    @RequestMapping("add")
    public int add(int goodsid, HttpSession session){
        return cartService.add(new Cart(((User) session.getAttribute(SYS.SHOPPING_LOGIN_USER)).getId(),goodsid));
    }

    @ResponseBody
    @RequestMapping("subtract")
    public int subtract(int goodsid, HttpSession session){
        return cartService.subtract(new Cart(((User) session.getAttribute(SYS.SHOPPING_LOGIN_USER)).getId(),goodsid));
    }

    @ResponseBody
    @RequestMapping("count")
    public int getCount(Cart cart){
        return cartService.getCount(cart);
    }

    @ResponseBody
    @RequestMapping("useraddress")
    public List<Useraddress> getUserAddress(HttpSession session){
        return cartService.getUserAddress(((User) session.getAttribute(SYS.SHOPPING_LOGIN_USER)).getId());
    }

    @ResponseBody
    @RequestMapping("cartgoodsstate")
    public int cartgoodsstate(int goodsid,HttpSession session){
        return cartService.cartgoodsstate(((User) session.getAttribute(SYS.SHOPPING_LOGIN_USER)).getId(),goodsid);
    }

}
